<!DOCTYPE html>
<html>
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  <title>The source code</title>
  <link href="../resources/prettify/prettify.css" type="text/css" rel="stylesheet" />
  <script type="text/javascript" src="../resources/prettify/prettify.js"></script>
  <style type="text/css">
    .highlight { display: block; background-color: #ddd; }
  </style>
  <script type="text/javascript">
    function highlight() {
      document.getElementById(location.hash.replace(/#/, "")).className = "highlight";
    }
  </script>
</head>
<body onload="prettyPrint(); highlight();">
  <pre class="prettyprint lang-js"><span id='global-property-S-'>/**
</span> * @fileOverview 表单分组验证
 * @ignore
 */

var $ = require(&#39;jquery&#39;),
  CLS_ERROR = &#39;x-form-error&#39;,
  Valid = require(&#39;./valid&#39;);

<span id='BUI-Form-GroupValidView'> /**
</span> * @class BUI.Form.GroupValidView
 * @private
 * 表单分组验证视图
 * @extends BUI.Form.ValidView
 */
function GroupValidView(){

}

BUI.augment(GroupValidView,Valid.View,{
<span id='BUI-Form-GroupValidView-method-showError'>  /**
</span>   * 显示一条错误
   * @private
   * @param  {String} msg 错误信息
   */
  showError : function(msg,errorTpl,container){
    var errorMsg = BUI.substitute(errorTpl,{error : msg}),
         el = $(errorMsg);
      el.appendTo(container);
      el.addClass(CLS_ERROR);
  },
<span id='BUI-Form-GroupValidView-method-clearErrors'>  /**
</span>   * 清除错误
   */
  clearErrors : function(){
    var _self = this,
      errorContainer = _self.getErrorsContainer();
    errorContainer.children(&#39;.&#39; + CLS_ERROR).remove();
  }
});

<span id='BUI-Form-GroupValid'>/**
</span> * @class BUI.Form.GroupValid
 * 表单分组验证
 * @extends BUI.Form.Valid
 */
function GroupValid(){

}

GroupValid.ATTRS = ATTRS =BUI.merge(true,Valid.ATTRS,{
  events: {
    value : {
<span id='BUI-Form-GroupValid-event-validchange'>      /**
</span>       * @event
       * 验证结果发生改变，从true变成false或者相反
       * @param {Object} ev 事件对象
       * @param {Object} ev.target 触发事件的子控件
       * @param {Boolean} ev.valid 是否通过验证
       */
      validchange : true,
<span id='BUI-Form-GroupValid-event-change'>      /**
</span>       * @event
       * 值改变，仅当通过验证时触发
       * @param {Object} ev 事件对象
       * @param {Object} ev.target 触发事件的子控件
       */
      change : true
    }
  }
});

BUI.augment(GroupValid,Valid,{
  __bindUI : function(){
    var _self = this,
      validEvent =  &#39;validchange change&#39;;

    //当不需要显示子控件错误时，仅需要监听&#39;change&#39;事件即可
    _self.on(validEvent,function(ev){
      var sender = ev.target;
      if(sender != this &amp;&amp; _self.get(&#39;showError&#39;)){

        var valid = sender.isValid();
        //是否所有的子节点都进行过验证
        if(_self._hasAllChildrenValid()){
          valid = valid &amp;&amp; _self.isChildrenValid();
          if(valid){
            _self.validControl(_self.getRecord());
            valid = _self.isSelfValid();
          }
        }
        
        if(!valid){
          _self.showErrors();
        }else{
          _self.clearErrors();
        }
      }
    });
  },
<span id='BUI-Form-GroupValid-method-isValid'>  /**
</span>   * 是否通过验证
   */
  isValid : function(){
    if(this.get(&#39;disabled&#39;)){ //如果被禁用，则不进行验证，并且认为true
      return true;
    }
    var _self = this,
      isValid = _self.isChildrenValid();
    return isValid &amp;&amp; _self.isSelfValid();
  },
<span id='BUI-Form-GroupValid-method-valid'>  /**
</span>   * 进行验证
   */
  valid : function(){
    var _self = this,
      children = _self.get(&#39;children&#39;);
    if(_self.get(&#39;disabled&#39;)){ //禁用时不进行验证
      return;
    }
    BUI.each(children,function(item){
      if(!item.get(&#39;disabled&#39;)){
        item.valid();
      }
    });
  },
<span id='BUI-Form-GroupValid-method-_hasAllChildrenValid'>  /**
</span>   * 是否所有的子节点进行过校验,如果子节点
   * @private
   */
  _hasAllChildrenValid : function(){
    var _self = this,
      children = _self.get(&#39;children&#39;),
      rst = true;
    BUI.each(children,function(item){
      if(!item.get(&#39;disabled&#39;) &amp;&amp; item.get(&#39;hasValid&#39;) === false){
        rst = false;
        return false;
      }
    });  
    return rst;
  },
<span id='BUI-Form-GroupValid-method-isChildrenValid'>  /**
</span>   * 所有子控件是否通过验证
   * @protected
   * @return {Boolean} 所有子控件是否通过验证
   */
  isChildrenValid : function(){
    var _self = this,
      children = _self.get(&#39;children&#39;),
      isValid = true;

    BUI.each(children,function(item){
      if(!item.get(&#39;disabled&#39;) &amp;&amp; !item.isValid()){
        isValid = false;
        return false;
      }
    });
    return isValid;
  },
  isSelfValid : function () {
    return !this.get(&#39;error&#39;);
  },
<span id='BUI-Form-GroupValid-method-validControl'>  /**
</span>   * 验证控件内容
   * @protected
   * @return {Boolean} 是否通过验证
   */
  validControl : function (record) {
    var _self = this,
      error = _self.getValidError(record);
    _self.set(&#39;error&#39;,error);
  },
<span id='BUI-Form-GroupValid-method-getErrors'>  /**
</span>   * 获取验证出错信息，包括自身和子控件的验证错误信息
   * @return {Array} 出错信息
   */
  getErrors : function(){
    var _self = this,
      children = _self.get(&#39;children&#39;),
      showChildError = _self.get(&#39;showChildError&#39;),
      validError = null,
      rst = [];
    if(showChildError){
      BUI.each(children,function(child){
        if(child.getErrors){
          rst = rst.concat(child.getErrors());
        }
      });
    }
    //如果所有子控件通过验证，才显示自己的错误
    if(_self._hasAllChildrenValid() &amp;&amp; _self.isChildrenValid()){
      validError = _self.get(&#39;error&#39;);
      if(validError){
        rst.push(validError);
      }
    }
    
    return rst;
  },  
  //设置错误模板时，覆盖子控件设置的错误模板
  _uiSetErrorTpl : function(v){
    var _self = this,
      children = _self.get(&#39;children&#39;);

    BUI.each(children,function(item){
      if(!item.get(&#39;userConfig&#39;)[&#39;errorTpl&#39;]){ //未定义错误模板时
        item.set(&#39;errorTpl&#39;,v);
      }
    });
  }
});

GroupValid.View = GroupValidView;

module.exports = GroupValid;
</pre>
</body>
</html>
